﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GeoFly
{
    public class WaterVapor:WaterVaporBase
    {
        public WaterVapor(double tav, double elev)
        {
            dTav = tav;
            dSvp = SatuVapPressure();
            dElev = elev;
        }
        public WaterVapor(double tav)
        {
            dTav = tav;
            dSvp = SatuVapPressure();
            dElev = 500;
        }
        /// <summary>
        /// the slope of the stauration vapor pressure curve
        /// </summary>
        /// <returns></returns>
        public override double TmpVapCurveSlp()
        {
            double dlta = -1;
	        dlta = 4098*dSvp/(Math.Pow((dTav+237.3),2));
	        return dlta;
        }
        /// <summary>
        /// latent heat of vaporization(MJ/kg)
        /// </summary>
        /// <returns></returns>
        public override double LatHeatVapor()
        {
            double lmt = 0;
	        lmt = 2.501-2.361*0.001*dTav;
	        return lmt;
        }
        /// <summary>
        /// mean air pressure
        /// </summary>
        /// <returns></returns>
        public override double AirPressureInSite()//
        {
            double ap=0;
	        ap = 101.3-0.01152*dElev+(0.544e-6)*dElev*dElev;
	        return ap;
        }
        /// <summary>
        /// 根据汉江流域32个气象站数据整理，可算逐日气压
        /// </summary>
        /// <param name="dn"></param>
        /// <returns></returns>
        public double AirPressureInPlain(int dn)
        {
            double ap=0;
	        ap = 101.3 - 0.0109*dElev+(1.1702-0.0005*dElev)*Math.Cos(2*Math.PI*dn/365);
	        return ap;
        }
        /// <summary>
        /// psychrometric constant
        /// </summary>
        /// <returns></returns>
        public override double PsychroConst()
        {
            double ap = AirPressureInSite();
	        double lmt = LatHeatVapor();
            double gm = 1.013*0.001*ap/(0.622*lmt);
	        return gm;
        }
        public double PsychroConst(int dn)
        {
            double ap = AirPressureInPlain(dn);
	        double lmt = LatHeatVapor();
	        double gm= 0.00163*ap/lmt;
	        return gm;
        }
        /// <summary>
        /// saturation vapor pressure(kPa)
        /// </summary>
        /// <returns></returns>
        public override double SatuVapPressure()
        {
            return Math.Exp((16.78 * dTav - 116.9) / (dTav + 237.3));
        }
        /// <summary>
        /// saturation vapor pressure by Kojima method(hPa)
        /// </summary>
        /// <returns></returns>
        public double SatuVapPressureKojima()
        {
            double dsvap;
            if (dTav >= 0)
                dsvap = 6.11 *Math.Exp(7.5 * dTav / (237 + dTav));
            else
                dsvap = 6.11 *Math.Exp(9.5 * dTav / (265 + dTav));
            return dsvap;
        }
        /// <summary>
        /// actual vapor pressure(kPa)
        /// </summary>
        /// <param name="realhumd"></param>
        /// <returns></returns>
        public override double ActVapPressure(double realhumd)
        {
            return realhumd * dSvp;
        }
        /// <summary>
        /// actual vapor pressure(hPa)
        /// </summary>
        /// <param name="realhumd"></param>
        /// <returns></returns>
        public double ActVapPressureKojima(double realhumd)//
        {
            double dsvap = SatuVapPressureKojima();
            return realhumd * dsvap;
        }
        /// <summary>
        /// relative humidity
        /// </summary>
        /// <param name="avp">actual vapor pressure</param>
        /// <returns></returns>
        public override double RelativeHmd(double avp)
        {
            return avp / dSvp;
        }
    }
}
    
